<template>
  <div>
    <el-dialog
      title="Google Merchant情况"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="80%"
      @close="close"
    >

      <div class="top">
        <el-input
          v-model="balance"
          clearable
          width="80"
          placeholder="余额低于:"
        />
        <el-input
          v-model="overdue"
          clearable
          width="80"
          placeholder="待审核:"
        />
        <el-checkbox v-model="success">未通过</el-checkbox>
        <!--        <el-select v-model="select_web" placeholder="选择网站" size="small" filterable clearable>-->
        <!--          <el-option label="所有" value=""></el-option>-->
        <!--          <el-option v-for="item in websites" :key="item.id" :label="item.domain"-->
        <!--                     :value="item.id"></el-option>-->
        <!--        </el-select>-->
        <single_website_select :website="select_web" :multiple="false" size="small" @change="function(v){select_web=v}" />
        <el-input
          v-model="kwInput"
          placeholder="输入 任意字符 进行筛选"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        </el-input>
        <el-button v-if="select_web !== ''" type="primary" icon="el-icon-edit" @click="handleMerchangConfig">merchant api</el-button>

      </div>

      <el-table v-loading.fullscreen.lock="listLoading" :data="data" border fit highlight-current-row style="width: 100%">

        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="Merchant ID::">
                <span>{{ props.row.merchant_id }}</span>
              </el-form-item>
              <el-form-item label="广告账户ID:">
                <span>{{ props.row.ads_id }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column align="center" width="100" prop="id" label="id" sortable="custom" />

        <el-table-column align="center" label="网站" prop="website__domain" sortable="custom" />
        <el-table-column align="center" width="120" label="通过" prop="success" sortable="custom">
          <template slot-scope="scope">
            <span v-if="scope.row.success > 0" style="color: green" @click="handleShowFeedProduct(scope.row)">{{ scope.row.success }}</span>
            <span v-else style="color: red" @click="handleShowFeedProduct(scope.row)">{{ scope.row.success }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="审核中" prop="waiting" sortable="custom">
          <template slot-scope="scope">
            <span v-if="scope.row.waiting > 0" style="color: red">{{ scope.row.waiting }}</span>
            <span v-else>{{ scope.row.waiting }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="未通过" prop="failed" sortable="custom" />
        <el-table-column align="center" width="120" label="即将过期" prop="overdue" sortable="custom">
          <template slot-scope="scope">
            <span v-if="scope.row.overdue > 0" style="color: red">{{ scope.row.overdue }}</span>
            <span v-else>{{ scope.row.overdue }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="240" label="同步时间" prop="sync_at" sortable="custom" />
        <el-table-column align="center" width="400" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleShowGoogleReportConfig(scope.row)">报告</el-button>
            <el-button size="mini" type="danger" @click="handleAction('delete', scope.row)">删除</el-button>
            <el-button size="mini" type="primary" @click="handleAction('edit', scope.row)">修改</el-button>
            <el-button size="mini" type="success" @click="handleFeed(scope.row)">Feed管理</el-button>
            <el-button size="mini" type="warning" :loading="syncloading" @click="handleSync(scope.row)">同步</el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
        v-if="data.length"
        background
        class="mt-pagination"
        :current-page="page"
        :page-sizes="[10,20,50,100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />

    </el-dialog>
    <merchantconfig
      :merchant-visible="merchantConfigVisual"
      :website_id="current_config_website_id"
      @closeMerchantDialog="merchantConfigVisual=false"
    />
    <merchantfeed
      :merchant-visible="merchantFeedVisual"
      :website_id="merchanfeedwebsite_id"
      @closeMerchantDialog="merchantFeedVisual=false"
    />
    <feedproduct
      :pst-visible="feedproductVisual"
      :website_id="merchanfeedwebsite_id"
      @closePstDialog="feedproductVisual=false"
    />
    <googleadsreportconfig
      :d-visible="googleReportConfigVisual"
      :googlemerchantconfig_id="current_config_id"
      @closeMyDialog="googleReportConfigVisual=false;"
    />

  </div>
</template>

<script>
import { Post, Put, Delete, Get, Sync } from '@/api/orders/merchantconfig'
// import { Get as GetWebsite } from '@/api/erp/elicink/website'

import qs from 'qs'
import Merchantconfig from './merchantconfig'
import Merchantfeed from './merchantfeed'
import Feedproduct from './feedproduct'
import Googleadsreportconfig from './googleadsreportconfig'
import Single_website_select from '../../erp/elicink/elements/single_website_select'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Merchant',
  components: { Single_website_select, Feedproduct, Merchantfeed, Merchantconfig, Googleadsreportconfig },
  props: ['merchantVisible', 'website_id'],
  data() {
    return {
      current_config_id: '',
      googleReportConfigVisual: false,
      feedproductVisual: false,
      syncloading: false,
      merchantFeedVisual: false,
      merchanfeedwebsite_id: '',
      merchantConfigVisual: false,
      websites: [],
      select_web: '',
      current_config_website_id: '',
      balance: '',
      success: false,
      overdue: '',
      data: [],
      listLoading: false,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,

      action: null,
      dialogVisible: false,
      title: '',
      temp: {
        domain: '',
        username: '',
        password: '',
        project_id: ''
      },
      width: '',
      projects: [],
      innerDialogVisible: false
    }
  },
  watch: {
    merchantVisible(v) {
      this.dialogVisible = v
      // if (v) {
      //   this.handleGetWebsite()
      // }
    },
    website_id(v) {
      if (v) {
        this.select_web = v
        this.getList()
      }
    }
  },
  methods: {

    handleShowGoogleReportConfig(row) {
      this.current_config_id = row.id
      this.googleReportConfigVisual = true
    },

    // handleGetWebsite() {
    //   GetWebsite().then(response => {
    //     this.websites = response.data.data;
    //   })
    // },
    resetTemp() {
      this.temp = {
        domain: '',
        username: '',
        password: '',
        project_id: ''
      }
    },
    getList() {
      console.log('merchagne 1111 send')
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        website_id: this.select_web,
        success: this.success,
        overdue: this.overdue,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    handleShowFeedProduct(row) {
      this.merchanfeedwebsite_id = row.website_id
      this.feedproductVisual = true
    },
    handleFeed(row) {
      this.merchanfeedwebsite_id = row.website_id
      this.merchantFeedVisual = true
    },
    handleSync(row) {
      this.syncloading = true
      Sync(row.id).then(response => {
        this.getList()
        this.notify('发送命令成功')
        this.syncloading = false
      }).catch(err => {
        console.log(err)
        this.syncloading = false
      })
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.width = '50%'
          this.title = row
          this.resetTemp()
          this.innerDialogVisible = true
          break
        case 'delete':
          this.width = '30%'
          this.title = `确认要删除${row.domain}?`
          this.temp = Object.assign({}, row)
          this.innerDialogVisible = true
          break
        case 'edit':
          this.current_config_website_id = row.website_id
          this.merchantConfigVisual = true
          // this.width = '50%'
          // this.title = `修改站点${row.domain}`;
          // this.temp = Object.assign({}, row)
          // this.innerDialogVisible = true;
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          if (_data.project_id === '') {
            alert('必须选择项目')
            return
          }
          if (!_data.domain.length) {
            alert('必须填写域名')
            return
          }
          if (!_data.username.length) {
            alert('必须填写用户名')
            return
          }
          if (!_data.password.length) {
            alert('必须填写密码')
            return
          }
          Post({
            domain: _data.domain,
            username: _data.username,
            password: _data.password,
            project_id: _data.project_id

          }
          ).then(response => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('添加成功')
          })
          break
        case
          'delete'
          :
          Delete(_data.id).then((response) => {
            this.innerDialogVisible = false
            this.getList()
            this.$emit('reload')
            this.notify('删除成功')
          })
          break
        case
          'edit'
          :
          Put(_data.id, {
            domain: _data.domain,
            username: _data.username,
            password: _data.password,
            project_id: _data.project_id
          }).then((response) => {
            this.innerDialogVisible = false
            this.$emit('reload')
            this.getList()
            this.notify('更新成功')
          })
          break
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    // 配置merchan
    handleMerchangConfig() {
      this.merchantConfigVisual = true
      this.current_config_website_id = this.select_web
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeMerchantDialog', false)
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-input {
    width: auto;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 5px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 5px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 10px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 60px;
    padding-top: 5px;
  }

  .top >>> .el-select {
    width: 300px;
  }
</style>
